package com.shire.shire_interface.controller.welcome;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.group.WxGroup;
import com.shire.shire_interface.model.group.WxGroupExample;
import com.shire.shire_interface.model.welcome.WxWelcomTask;
import com.shire.shire_interface.model.welcome.WxWelcomTaskExample;
import com.shire.shire_interface.model.welcome.WxWelcomeSetting;
import com.shire.shire_interface.model.welcome.WxWelcomeSettingExample;
import com.shire.shire_interface.service.group.WxGroupService;
import com.shire.shire_interface.service.welcome.WxWelcomeSettingService;
import com.shire.shire_interface.service.welcome.WxWelcomeTaskService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Api(tags = "群管理 进群欢迎")
@RestController
@RequestMapping("/welcome")
public class WxWelcomeTaskController extends BaseController {

    @Resource
    private WxWelcomeSettingService wxWelcomeSettingService;
    @Resource
    private WxWelcomeTaskService wxWelcomeTaskService;
    @Resource
    private WxGroupService wxGroupService;


//    @ApiOperation(value = "已设置欢迎语的群列表csId, wxaccount")
    @ResponseBody
    @PostMapping("/welcomeList")
    public Response welcomeList(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();

        try {
            Integer csId = jsonObject.getInteger("csId");

            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            String wxaccount = jsonObject.getString("wxaccount");
            WxWelcomeSettingExample wxWelcomeSettingExample = new WxWelcomeSettingExample();
            WxWelcomeSettingExample.Criteria criteria = wxWelcomeSettingExample.createCriteria().andStatusEqualTo(0);
            if (StringUtils.isNotBlank(wxaccount))
                criteria.andWxaccountidEqualTo(wxaccount);
            wxWelcomeSettingExample.setOrderByClause("id desc");
            List<WxWelcomeSetting> wxWelcomeSettings = wxWelcomeSettingService.selectByExample(wxWelcomeSettingExample);

            if (wxWelcomeSettings.size()<1){
                response.success("操作成功");
                return response;
            }

            List<Integer> settingIdList = new ArrayList<>();
            for (WxWelcomeSetting setting : wxWelcomeSettings){
                settingIdList.add(setting.getId());
            }

            WxWelcomTaskExample wxWelcomTaskExample = new WxWelcomTaskExample();
            wxWelcomTaskExample.createCriteria().andSettingidIn(settingIdList);
            List<WxWelcomTask> wxWelcomTasks = wxWelcomeTaskService.selectByExample(wxWelcomTaskExample);

            List<String> groupNoList = new ArrayList<>();
            for (WxWelcomTask task : wxWelcomTasks){
                groupNoList.add(task.getWxgroupid());
            }
            WxGroupExample wxGroupExample = new WxGroupExample();
            wxGroupExample.createCriteria().andWxaccountEqualTo(wxaccount).andGroupnoIn(groupNoList);

            Map<String, WxGroup> groupMap = new HashMap<>();
            for (WxGroup group : wxGroupService.selectByExample(wxGroupExample)){
                groupMap.put(group.getGroupno(),group);
            }



            for (WxWelcomeSetting wxWelcomeSetting : wxWelcomeSettings) {
                List<WxGroup> groups = new ArrayList<>();
                for (WxWelcomTask task : wxWelcomTasks){
                    if (task.getSettingid().equals(wxWelcomeSetting.getId())){
                        WxGroup wxGroup = groupMap.get(task.getWxgroupid());
                        if (wxGroup == null)
                            continue;
                        WxGroup group = (WxGroup) wxGroup.clone();
                        group.setMsgCnt(task.getMsgcount());
                        groups.add(group);
                    }
                }

                wxWelcomeSetting.setGroupList(groups);

            }
            response.setData(wxWelcomeSettings);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }
}
